define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'gateway/index',
                    add_url: 'gateway/add',
                    edit_url: 'gateway/edit',
                    del_url: 'gateway/del',
                    multi_url: 'gateway/multi',
                    table: 'gateway',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showExport: true,
                exportTypes: ['excel'],
                showColumns: false,
                columns: [
                    [{
                            checkbox: true
                        },
                        {
                            field: 'id',
                            title: __('Id')
                        },
                        /*{field: 'admin_id', title: __('Admin_id')},*/
                        {
                            field: 'admin.nickname',
                            title: __('所属用户'),
                            formatter: Table.api.formatter.label
                        },
                        {
                            field: 'name',
                            title: __('Name')
                        },
                        {
                            field: 'location',
                            title: __('Location')
                        },
                        {
                            field: 'state',
                            title: __('State'),
                            visible: false,
                            searchList: {
                                "1": __('State 1'),
                                "0": __('State 0')
                            }
                        },
                        {
                            field: 'state_text',
                            title: __('State'),
                            operate: false
                        },
                        {
                            field: 'concurrency',
                            title: __('Concurrency')
                        },
                        {
                            field: 'max_concurrency',
                            title: __('Max_concurrency')
                        },
                        {
                            field: 'username',
                            title: __('Username')
                        },
                        {
                            field: 'password',
                            title: __('Password')
                        },
                        {
                            field: 'register',
                            title: __('Register'),
                            visible: false,
                            searchList: {
                                "receive": __('register receive'),
                                "send": __('register send')
                            }
                        },
                        //{field: 'register_text', title: __('Register'), operate:false},
                        {
                            field: 'reg_state',
                            title: __('注册状态')
                        },
                        /*{field: 'register_proxy', title: __('Register_proxy')},
                        {field: 'proxy', title: __('Proxy')},
                        {field: 'domain', title: __('Domain')},
                        {field: 'expires', title: __('Expires')},*/
                        {
                            field: 'call_interval',
                            title: __('Call_interval')
                        },
                        {
                            field: 'last_call_time',
                            title: __('Last_call_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        /*{field: 'concurrency_calls', title: __('Concurrency_calls')},*/
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        updateView() {
            var register = $('#c-register').val();
            switch (register) {
                case 'send':
                    $('#c-register_proxy').parent().parent().removeClass('hidden');
                    $('form').validator("setField", "row[register_proxy]", "required;");
                    $('#c-proxy').parent().parent().removeClass('hidden');
                    $('form').validator("setField", "row[proxy]", "required;");
                    $('#c-domain').parent().parent().removeClass('hidden');
                    $('form').validator("setField", "row[domain]", "required;");
                    $('#c-expires').parent().parent().removeClass('hidden');
                    $('form').validator("setField", "row[expires]", "required;");
                    $('form').validator("setField", "row[username]", "required;");
                    $('form').validator("setField", "row[password]", "required;");
                    break;
                case 'receive':
                    $('#c-register_proxy').parent().parent().addClass('hidden');
                    $('form').validator("setField", "row[register_proxy]", null);
                    $('#c-proxy').parent().parent().addClass('hidden');
                    $('form').validator("setField", "row[proxy]", null);
                    $('#c-domain').parent().parent().addClass('hidden');
                    $('form').validator("setField", "row[domain]", null);
                    $('#c-expires').parent().parent().addClass('hidden');
                    $('form').validator("setField", "row[expires]", null);
                    $('form').validator("setField", "row[username]", "required;");
                    $('form').validator("setField", "row[password]", "required;");
                    break;
                case 'none':
                    $('#c-register_proxy').parent().parent().removeClass('hidden');
                    $('form').validator("setField", "row[register_proxy]", "required;");
                    $('#c-proxy').parent().parent().removeClass('hidden');
                    $('form').validator("setField", "row[proxy]", "required;");
                    $('#c-domain').parent().parent().removeClass('hidden');
                    $('form').validator("setField", "row[domain]", "required;");
                    $('#c-expires').parent().parent().removeClass('hidden');
                    $('form').validator("setField", "row[expires]", "required;");
                    $('form').validator("setField", "row[username]", null);
                    $('form').validator("setField", "row[password]", null);
                    break;
            }
        },
        add: function () {
            Controller.api.bindevent();
            $('#c-name').change(function () {
                $('#c-username').val($(this).val());
            });
            $('#c-register').change(function () {
                Controller.updateView();
            });
            Controller.updateView();
        },
        edit: function () {
            Controller.api.bindevent();
            $('#c-name').change(function () {
                $('#c-username').val($(this).val());
            });
            $('#c-register').change(function () {
                Controller.updateView();
            });
            Controller.updateView();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});